/**
 * GTHttpRequestTest.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "GTHttpRequest.h"

@interface GTHttpRequestTest : SenTestCase {
}
@end

@implementation GTHttpRequestTest

- (void) testSuccessfulCallOfHttpGet {
  GTHttpRequest *request = [GTHttpRequest requestWithUrl: @"http://mobile.viaggiatreno.it/viaggiatreno/mobile/numero"];
  STAssertEquals([request sendHttpGet], GTHttpStatusCodeOk, @"Wrong status code");
  NSString *result = [request httpResult];
  STAssertNotNil(result, @"there should be a result");
  STAssertTrue([result rangeOfString: @"<html xmlns=\"http://www.w3.org/1999/xhtml\">"].location != NSNotFound, @"Result should contains html");
}

- (void) testSuccessfulCallOfHttpGetWithParameters {
  GTHttpRequest *request = [GTHttpRequest requestWithUrl: @"http://viaggiatreno.it/viaggiatreno/mobile/scheda"];
	[request setParameter: @"lang" withValue: @"IT"];
	[request setParameter: @"dettaglio" withValue: @"visualizza"];
	[request setParameter: @"numeroTreno" withValue: @"2005"];
	[request setParameter: @"tipoRicerca" withValue: @"numero"];
  STAssertEquals([request sendHttpGet], GTHttpStatusCodeOk, @"Wrong status code");  
  NSString *result = [request httpResult];
  STAssertNotNil(result, @"there should be a result");
  STAssertTrue([result rangeOfString: @"<html xmlns=\"http://www.w3.org/1999/xhtml\">"].location != NSNotFound, @"Result should contains html");
  STAssertTrue([result rangeOfString: @"2005"].location != NSNotFound, @"Result should contains train number in parameter");
}

- (void) testSuccessfulCallOfHttpGetWithEscapedChars {
  GTHttpRequest *request = [GTHttpRequest requestWithUrl:@"http://www.volagratis.com/suggester/proxy/airports"];
	[request setParameter:@"key" withValue:@"new "];
	[request setParameter:@"limit" withValue:@"20"];
	[request setParameter:@"language" withValue:@"en"];
  
  STAssertEquals([request sendHttpGet], GTHttpStatusCodeOk, @"Wrong status code");
  NSString *result = [request httpResult];
  STAssertNotNil(result, @"there should be a result");
  STAssertTrue([result rangeOfString: @"New "].location != NSNotFound, @"Result should contains the searched string");
}

- (void)testFailedCallOfHttpGetForHostNotFound {
	GTHttpRequest *request = [GTHttpRequest requestWithUrl:@"http://asdfasdasdf"];
  STAssertEquals([request sendHttpGet], GTHttpStatusCodeError, @"Wrong status code");
  NSString *result = [request httpResult];
  STAssertNil(result, @"there shouldn't be a result");
	STAssertTrue([[request errorDescription] length] > 0, @"There should be an error description");
}

- (void) testSuccessfulCallOfHttpPost {
  GTHttpRequest *request = [GTHttpRequest requestWithUrl: @"http://mobile.viaggiatreno.it/viaggiatreno/mobile/numero"];
	[request setParameter: @"lang" withValue: @"IT"];
	[request setParameter: @"numeroTreno" withValue: @"2005"];
	[request setParameter: @"tipoRicerca" withValue:@"numero"];  
  STAssertEquals([request sendHttpGet], GTHttpStatusCodeOk, @"Wrong status code");
  NSString *result = [request httpResult];
  STAssertNotNil(result, @"there should be a result");
  STAssertTrue([result rangeOfString: @"<html xmlns=\"http://www.w3.org/1999/xhtml\">"].location != NSNotFound, @"Result should contains html");
  STAssertTrue([result rangeOfString: @"2005"].location != NSNotFound, @"Result should contains train number in parameter");
}

@end